<?php
		
	define('TABLE', 'Table of Contents');
	define('SPECIFIC', 'Seasonal Missions');

	include_once('../../config.php'); 
	session_start();
	LinkDataBase();
	$Array_XMLSub = CatchDBdate($_POST['season'], $_POST['tablename'], $_POST['step']);
	$Array_DBTableName = DBTableNameGet($_SESSION['ID'], $_POST['year'], $_POST['season'], $_POST['tablename']);
	$filename = $Array_DBTableName[0];
	$TableQAName = str_ireplace($_POST['tablename'].' - ','',$Array_DBTableName[1]);
?>
<script type="text/javascript">
	<?php
	//Check that load page first. 
	if (!isset($_POST['Category'])) {
	?>
		$(function() {
			
			CateClicked(null);
		});
	<?php
	}
	?>
	
	function CateClicked(href) {
		
		var Category;
		var hrefID;
		
		if (href==null) {
			Category = 'Total';
			hrefID = 'href-1';
		}
		else {
			Category = $(href).text();
			hrefID = $(href).attr('id');
		}
		
		//Prevent that reveal error message.
		<?php
		if (isset($_POST['tablename']))
			$tablename = $_POST['tablename'];
		else
			$tablename = '';
		if (isset($_POST['keyword']))
			$keyword = $_POST['keyword'];
		else
			$keyword = '';
		if (isset($_POST['contain']))
			$contain = $_POST['contain'];
		else
			$contain = '';
		if (isset($_POST['Xaxis']))
			$Xaxis = $_POST['Xaxis'];
		else
			$Xaxis = '';
		if (isset($_POST['step']))
			$step = $_POST['step'];
		else
			$step = 0;
			
		?>
		var Year = '<?php echo $_POST['year']; ?>';
		var Season = '<?php echo $_POST['season']; ?>';
		var Type = '<?php echo $_POST['type']; ?>';
		var TableName = '<?php echo $tablename; ?>';
		var KeyWord = '<?php echo $keyword; ?>';
		var Contain = '<?php echo $contain; ?>';
		var Xaxis = '<?php echo $Xaxis; ?>';
		var FileName = '<?php echo $filename; ?>';
		var Step = '<?php echo $step; ?>';
		
		//Catch SubXML info.
		var Array_SubXML;
		<?php
		if (!empty($Array_XMLSub)) {
		?>
			Array_SubXML = <?php echo json_encode($Array_XMLSub); ?>;
		<?php
		}
		else {
		?>
			Array_SubXML = '';
		<?php
		}
		?>
		
		//Catch and create breadcrumb.
		var Array_Previous_Title = new Array();
		<?php
		if (($_POST['type']!=TABLE)&&($_POST['type']!=SPECIFIC)) {
			if (!is_array($_POST['previous_title'])) {
				?>
				Array_Previous_Title.push('<?php echo $_POST['previous_title']; ?>');
				<?php
				$previous_title = $_POST['previous_title'];
			}
			else {
				?>
				Array_Previous_Title = <?php echo json_encode($_POST['previous_title']); ?>;
				<?php
				$previous_title = $_POST['previous_title'][0];
				for ($i=1; $i<count($_POST['previous_title']); $i++)
					$previous_title = $previous_title.'/'.$_POST['previous_title'][$i][0];
			}
		}
		else if (is_array($_POST['previous_title'])) {
			?>
			Array_Previous_Title = <?php echo json_encode($_POST['previous_title']); ?>;
			<?php
			$previous_title = $_POST['previous_title'][0];
			for ($i=1; $i<count($_POST['previous_title']); $i++)
				$previous_title = $previous_title.'/'.$_POST['previous_title'][$i][0];
		}
		?>
		
		var ChartType = '<?php echo $_POST['charttype']; ?>';
		
		if ((ChartType=='Word')||(ChartType=='Radial'))
			ChartType = 'Bar';
		
		$("#graphic_div").load('../chart/detail/'+ChartType+'Chart.php',
		{'year':Year, 'season':Season, 'type':Type, 'Category':Category, 'tablename':TableName, 
		'keyword':KeyWord, 'contain':Contain, 'Xaxis':Xaxis, 'filename':FileName,
		 'Array_SubXML': Array_SubXML, 'step':Step, 'previous_title':Array_Previous_Title},
			function()
			{
				$('.overview-menu-active').attr('class','overview-menu');
				$('#'+hrefID).attr('class','overview-menu-active');
			}
		);
	}
	
	function PreviousPress() {
		var Step = <?php echo $_POST['step']; ?>;
		var year = '<?php echo $_POST['year']; ?>';
		var season = '<?php echo $_POST['season']; ?>';
		var type = '<?php echo $_POST['type']; ?>';
		console.log(type);
		if (Step==1) {
			
			if ((type=='Table of Contents')||(type=='Seasonal Missions'))
				$("#content-detail").load('../table/index.php',{'year': year, 'season': season, 'type': type});
			else
				$("#content-detail").load('../chart/index.php',{'year': year, 'season': season, 'type': type});
		}
		else {
			
			var Array_Previous_Title = new Array();
			var Temp_Array_Previous_Title = <?php echo json_encode($_POST['previous_title']); ?>;
			console.log(Temp_Array_Previous_Title);
			var TableNumber = Temp_Array_Previous_Title[Step-1][1];
			var ChartType = Temp_Array_Previous_Title[Step-1][2];
			var keyword = Temp_Array_Previous_Title[Step-1][3];
			var contain = Temp_Array_Previous_Title[Step-1][4];
			var Xaxis = Temp_Array_Previous_Title[Step-1][5];
			
			for (var i=0; i<Step-1; i++)
				Array_Previous_Title.push(Temp_Array_Previous_Title[i]);
			
			$("#content-detail").load('../chart/detail/index.php',
			{'year':year, 'season':season, 'type':type, 'tablename': TableNumber, 'charttype': ChartType, 
			'keyword': keyword, 'contain': contain, 'Xaxis': Xaxis, 'step': Step-1, 'previous_title': Array_Previous_Title});
		}
	}
	
	function GotoBegin() {
		
		var year = '<?php echo $_POST['year']; ?>';
		var season = '<?php echo $_POST['season']; ?>';
		var type = '<?php echo $_POST['type']; ?>';
		
		if ((type=='Table of Contents')||(type=='Seasonal Missions'))
			$("#content-detail").load('../table/index.php',{'year': year, 'season': season, 'type': type});
		else
			$("#content-detail").load('../chart/index.php',{'year': year, 'season': season, 'type': type});
	}
	
	function GotoTargetLocation(href) {
		
		var year = '<?php echo $_POST['year']; ?>';
		var season = '<?php echo $_POST['season']; ?>';
		var type = '<?php echo $_POST['type']; ?>';
		
		var Step = $(href).attr('id');
		var TableNumber = $(href).attr('name');
		var TableQAName = $(href).text();
		
		var Array_Previous_Title = new Array();
		var Temp_Array_Previous_Title = <?php echo json_encode($_POST['previous_title']); ?>;
		
		var ChartType = Temp_Array_Previous_Title[Step][2];
		var keyword = Temp_Array_Previous_Title[Step][3];
		var contain = Temp_Array_Previous_Title[Step][4];
		var Xaxis = Temp_Array_Previous_Title[Step][5];
		
		for (var i=0; i<Step; i++)
			Array_Previous_Title.push(Temp_Array_Previous_Title[i]);
				
		$("#content-detail").load('../chart/detail/index.php',
		{'year':year, 'season':season, 'type':type, 'tablename': TableNumber, 'charttype': ChartType, 
		'keyword': keyword, 'contain': contain, 'Xaxis': Xaxis, 'step': Step, 'previous_title': Array_Previous_Title});
	}
	
</script>
<div class="container2">
	<div>
      <table width="1024px" border="0" cellspacing="0" cellpadding="0">
        <tr>
        
        <?php 
		if (($_POST['type']!=TABLE)&&($_POST['type']!=SPECIFIC)) {
		?>
          <td width="75%" align="left" valign="top"><p class="table-header"><?php echo $previous_title; ?>/</p>
          <p class="table-subheader"><?php echo $TableQAName; ?></p></td>
        <?php
		}
		else {
		?>
          <td width="75%" align="left" valign="top"><p class="table-header"><?php echo $previous_title; ?>
          
          <?php
		  if ($_POST['step']!=1) {
		  ?>
          /
          <?php
		  }
		  ?>
		  
		  <?php echo $TableQAName; ?></p></td>
        <?php
		}
		?>
          <td width="25%" align="right" valign="top" class="table-link"><a href="#" class="table-link" onClick="PreviousPress();"><span class="table-arrow-link">&lt;</span> Previous </a></td>
        </tr>
	  </table>
	</div>
	<div style="float:left;" id="graphic_div"></div> 
	
	<div style="position:absolute;"> 
     <table border="0" cellspacing="0" cellpadding="0" style="margin: 0 10px 30px 0;">
		
        <tr>
          <td><a href="#" class="overview-menu">Categories</a></td>
        </tr>
        <tr>
          <td height="10px" class="hr1"></td>
        </tr>
        <tr>
          <td><a href="#" id="href-1" class="overview-menu-active" onclick="CateClicked(this);">Total</a></td>
        </tr>
        <tr>
          <td><a href="#" id="href-2" class="overview-menu" onclick="CateClicked(this);">Gender</a></td>
        </tr>
        <tr>
          <td><a href="#" id="href-3" class="overview-menu" onclick="CateClicked(this);">Age</a></td>
        </tr>
        <tr>
          <td><a href="#" id="href-4" class="overview-menu" onclick="CateClicked(this);">Region</a></td>
        </tr>
        <tr>
          <td><a href="#" id="href-5" class="overview-menu" onclick="CateClicked(this);">Income</a></td>
        </tr>
        <tr>
          <td><a href="#" id="href-6" class="overview-menu" onclick="CateClicked(this);">Marital Status</a></td>
        </tr>
        <tr>
          <td><a href="#" id="href-7" class="overview-menu" onclick="CateClicked(this);">Kids in HH</a></td>
        </tr>
        <tr>
          <td><a href="#" id="href-8" class="overview-menu" onclick="CateClicked(this);">Age of Children</a></td>
        </tr>
        <tr>
          <td><a href="#" id="href-9" class="overview-menu" onclick="CateClicked(this);">Race</a></td>
        </tr>
        <tr>
          <td><a href="#" id="href-10" class="overview-menu" onclick="CateClicked(this);">Ethnicity</a></td>
        </tr>
        <tr>
          <td class="hr1"></td>
        </tr>
        <tr>
          <td height="50px" style="padding-left:15px; margin-bottom:200px;"><button class="button downloadcsv white"onclick="window.location='../table/csv.php?filename=<?php echo $filename."&sheet=".$tablename."&Qname=".str_replace("'","\'",$TableQAName);?>'">Download as csv</button></td>
         <tr>
          <td height="80px" style="padding-left:15px; margin-bottom:200px;">&nbsp;</td>
        </tr>
        </tr>
	  </table> 
	</div>
     
</div>
